package com.shop.item.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemCollectionInput;
import com.shop.common.model.result.item.ItemCollectionOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.state.DataStatus;
import com.shop.item.atom.atomsvc.IItemCollectionAtomService;
import com.shop.item.atom.dao.ItemCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemCollectionAtomService extends BaseService implements IItemCollectionAtomService {

    @Autowired
    private ItemCollectionMapper collectionMapper;

    @Override
    public ServiceBean<Integer> insertItemCollection(ItemCollectionInput collectionInput) {
        int num = collectionMapper.insertSelective(collectionInput);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteItemCollection(ItemCollectionInput collectionInput) {
        collectionInput.setDataStatus(DataStatus.DELETE.value());
        int num = collectionMapper.deleteByPrimaryKey(collectionInput);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<ItemCollectionOutput> selectItemCollection(ItemCollectionInput collectionInput) {
        List<ItemCollectionOutput> list = collectionMapper.selectList(collectionInput);
        if (list == null || list.size() <= 0) {
            return resultFailed();
        }
        return resultData(list.get(0));
    }

    @Override
    public ServiceBean<List<ItemCollectionOutput>> selectItemCollectionList(ItemCollectionInput collectionInput) {
        List<ItemCollectionOutput> list = collectionMapper.selectList(collectionInput);
        if (list == null || list.size() <= 0) {
            return resultFailed();
        }
        return resultData(list);
    }

    @Override
    public ServiceBean<List<ItemCollectionOutput>> selectItemCollectionList4P(InputBean<ItemCollectionInput> inputBean) {
        ItemCollectionInput itemCollectionInput = inputBean.getParam();
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<ItemCollectionOutput> list = collectionMapper.selectList(itemCollectionInput);
        PageInfo<ItemCollectionOutput> pageInfo = new PageInfo<>(list);
        if (pageInfo.getList() == null || pageInfo.getList().size() <= 0) {
            return resultFailed();
        }
        ServiceBean serviceBean = new ServiceBean(pageInfo.getList());
        serviceBean.setMaxSize(pageInfo.getTotal());
        serviceBean.setSuccess(true);
        return serviceBean;
    }

    @Override
    public ServiceBean<String> updateItemCollection(ItemCollectionInput collectionInput) {
        int num = collectionMapper.updateByPrimaryKeySelective(collectionInput);
        if (opSuccess(num)) {
            return resultSuccess();
        }
        return resultFailed();
    }
}
